import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private readonly userRepository: Repository<User>,
  ) {}

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  create(createUserDto: CreateUserDto) {
    return 'This action adds a new user';
  }

  async findAll() {
    return await this.userRepository.find();
  }

  async findOne(id: number) {
    const user = await this.userRepository.findOneBy({
      id: id,
    });

    const res = { data: { ...user }, msg: '查询成功' };
    console.log('findOne', res);
    return res;
  }
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  changeStatus(id: number, updateUserDto: UpdateUserDto) {
    return `This action put a #${id} user`;
  }

  async remove(id: number) {
    const res = await this.findOne(id);
    if (!res.data) {
      throw new Error('未找到该用户');
    }
    this.userRepository
      .remove(res.data)
      .then((res) => {
        console.log('res', res);
        return {
          status: 200,
          msg: '成功',
        };
      })
      .catch((err) => {
        console.log('err', err);
        return {
          status: 10001,
          msg: '失败',
        };
      });
  }
}
